package cn.wolfcode.service.impl;

import cn.wolfcode.domain.SystemDictionary;
import cn.wolfcode.mapper.SystemDictionaryMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.ISystemDictionaryService;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class SystemDictionaryServiceImpl implements ISystemDictionaryService {

    @Autowired
    private SystemDictionaryMapper systemDictionaryMapper;

    @Override
    @Transactional
    public void save(SystemDictionary systemDictionary) {
        systemDictionaryMapper.insert(systemDictionary);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        systemDictionaryMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public void update(SystemDictionary systemDictionary) {
        systemDictionaryMapper.updateByPrimaryKey(systemDictionary);
    }

    @Override
    public SystemDictionary get(Long id) {
        return systemDictionaryMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<SystemDictionary> listAll() {
        return systemDictionaryMapper.selectAll();
    }

    @Override
    public PageInfo<SystemDictionary> query(QueryObject qo) {
        // 告诉插件我们查询的是第几页，每页多少条
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        // 查询当前页的数据
        List<SystemDictionary> systemDictionarys = systemDictionaryMapper.selectForList(qo);
        return new PageInfo<>(systemDictionarys);
    }

    @Override
    public List<Map> queryTreeData() {
        //查询顶级数据目录(父节点)-parent_id 为null
        List<SystemDictionary> all = systemDictionaryMapper.selectAll();
        List<SystemDictionary> top = new ArrayList<>();
        for (SystemDictionary item : all) {
            if(item.getParentId() == null){
                top.add(item);
                item.setChildren(findChildren(all, item.getId()));
            }
        }
        //查询二级(下级)数据, 查询儿子(子节点)
        fillChildren(all, top);


        //页面的treeview插件用到属性: text 跟nodes
        //而查询出来数据是name, children
        //思考: 如何进行转换?
        //方案1: 使用map进行数据改造[推荐]
        String str = JSON.toJSONString(top);
        str = str.replaceAll("name", "text").replaceAll("children", "nodes");
        //参数1:list集合json格式字符串, 参数2:集合泛型
        List<Map> maps = JSON.parseArray(str, Map.class);

        //方案2:修改数据字典表(不建议)
        return maps;
    }
    //填充儿子
    private void fillChildren(List<SystemDictionary> all, List<SystemDictionary> children) {
        if(children == null || children.size() == 0){
            return;
        }
        for (SystemDictionary item : children) {
            //找儿子
            item.setChildren(findChildren(all, item.getId()));
            //儿子找儿子
            fillChildren(all, item.getChildren());
        }
    }

    //参数1:所有数据, 参数2:父id
    private List<SystemDictionary>  findChildren(List<SystemDictionary> all, Long parentId){
        List<SystemDictionary> children = new ArrayList<>();
        for (SystemDictionary item : all) {
            if(item.getParentId() == parentId){
                children.add(item);
            }
        }
        return  children;
    }


    @Override
    public List<SystemDictionary> queryBySn(String sn) {
        return systemDictionaryMapper.selectBySn(sn);
    }

    @Override
    public List<SystemDictionary> queryByParentId(Long parentId) {
        return systemDictionaryMapper.selectByParentId(parentId);
    }
}
